package cn.stronglink.asset.message.mq.dam.entity.dacai;

import java.util.Map;
import java.util.Map.Entry;

import cn.stronglink.asset.message.mq.dam.entity.ScreenCommandEntity;
import cn.stronglink.suite.core.util.ByteUtil;

public class UpdateArrayTextCommandFactory extends DacaiDefaultCommandFactory {
	protected final static String COMMAND_CODE = "B112";
	protected final static String CHANGE_SCREEN_COMMAND_CODE = "B100";
	/**
	 *  屏幕界面的Id
	 */
	protected short screenId;
	/**
	 * 所在机柜
	 */
	protected String rackConverCode;
	/**
	 * 传感器数组  key为显示控件id，value为显示内容
	 */
	protected Map<Short, String> sensorMap;
	
	public UpdateArrayTextCommandFactory(String rackConverCode,short screenId,Map<Short, String> sensorMap) {
		this.rackConverCode=rackConverCode;
		this.screenId=screenId;
		this.sensorMap=sensorMap;
	}

	@Override
	public ScreenCommandEntity createScreenCommand() throws Exception {
		StringBuffer sb = new StringBuffer(COMMAND_HEADER);
		sb.append(COMMAND_CODE);
		sb.append(ByteUtil.byteArrToHexString(ByteUtil.shortToByteArr(this.screenId)));  // 界面id
		for(Entry<Short,String> item : sensorMap.entrySet()) {
			sb.append(ByteUtil.byteArrToHexString(ByteUtil.shortToByteArr(item.getKey())));  // 控件id
			byte[] datas = item.getValue().getBytes("GBK");
			short dataLen = (short) datas.length;
			sb.append(ByteUtil.byteArrToHexString(ByteUtil.shortToByteArr(dataLen)));  // 控件内容长度
			sb.append(ByteUtil.byteArrToHexString(datas));  // 控件内容
		}
		sb.append(COMMAND_FOOTER);
		
		ScreenCommandEntity sc = new ScreenCommandEntity();
		sc.setCommand(sb.toString());
		sc.setRackConverCode(this.rackConverCode);
		
		sb.setLength(0);
		sb = null;
		
		return sc;
	}

	@Override
	public ScreenCommandEntity changeScreenCommand() throws Exception {
		StringBuffer sb = new StringBuffer(COMMAND_HEADER);
		sb.append(CHANGE_SCREEN_COMMAND_CODE);
		sb.append(ByteUtil.byteArrToHexString(ByteUtil.shortToByteArr(this.screenId)));  // 界面id
		sb.append(COMMAND_FOOTER);
		
		ScreenCommandEntity sc = new ScreenCommandEntity();
		sc.setCommand(sb.toString());
		sc.setRackConverCode(this.rackConverCode);
		
		sb.setLength(0);
		sb = null;
		
		return sc;
	}
}
